PHP - Get and Post

    PHP - Get and Post

    PHP allows you to create an environment variable that can be set by phpinfo.php page. HTTP_USER_AGENT is an environment variable provided by PHP that identifies the browser and the operating system. HTTP_USER_AGENT variable creates dynamic content based on the contained information. The value of these environment variables can be accessed via getenv() function. Also, you can generate a random number using the rand() function. This random number generator avoids the generation of a regular pattern of numbers which can be provided as the argument.

    How client data sent to the server

    Get and Post methods are used to send client-side information to the webserver. The data gets encoded using URL encoding before it is sent to the server. The name and value are joined with an equal sign while each pair is separated by an ampersand. Even the spaces are getting replaced with a + character. After these changes, the data is sent to the server.

    Example

    name1=value1&name2=value2

    1. Get method

    The encoded user information gets appended to the page information with the help of the GET method. The encoded data and the page information get separated with the "?” sign.

    Syntax

    https://www.techgeekbuzz.com/info.htm?name1=value1&name2=value2

    This method creates a string that will appear in the browser box whose length can be 1024 characters only. You cannot send images or word documents like information using the GET method. GET method data can be accessed using an environment variable called “QUERY_STRING”. This sent data can be accessed using the $_GET associative array.

    Example

    <?php
       if( $_GET["name"] || $_GET["age"] ) {
          echo "Hello ". $_GET['name']. "<br />";
          echo "You are ". $_GET['age']. " years old.";
          exit();
       }
    ?>
    <html>
       <body>
          <form action = "<?php $_PHP_SELF ?>" method = "GET">
             Name: <input type = "text" name = "name" />
             Age: <input type = "text" name = "age" />
             <input type = "submit" />
          </form>
       </body>
    </html>

    Output Form

    2. POST method

    HTTP headers are used to transfer the data with the POST method. The encoding method of the sent data or information is the same as the GET method instead the data is put in the header called “QUERY_STRING”. There is no size limit to the string of data you sent. Binary data and the ASCII data can also be sent. HTTP protocol handles data security. Unlike $_GET method, here we use the $_POST associative array to access all sent data.

    Example

    <?php
       if( $_POST["name"] || $_POST["age"] ) {
          echo "Hello ". $_POST['name']. "<br />";
          echo "You are ". $_POST['age']. " years old."; 
          exit();
       }
    ?>
    <html>
       <body>
       
          <form action = "<?php $_PHP_SELF ?>" method = "GET">
             Name: <input type = "text" name = "name" />
             Age: <input type = "text" name = "age" />
             <input type = "submit" />
          </form>
       </body>
    </html>

    Output Form

    $_REQUEST variable

    This variable has data for both of the $_GET and $_POST. This method will get the result from the data sent with GET and POST method.

    Example

    <?php
       if( $_REQUEST["name"] || $_REQUEST["age"] ) {
          echo "Hello ". $_REQUEST['name']. "<br />";
          echo "You are ". $_REQUEST['age']. " years old.";
          exit();
       }
    ?>
    <html>
       <body>
          <form action = "<?php $_PHP_SELF ?>" method = "POST">
             Name: <input type = "text" name = "name" />
             Age: <input type = "text" name = "age" />
             <input type = "submit" />
          </form>     
       </body>
    </html>

    Output Form

    People are also reading: